package com.example.androidkt.second

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.os.PersistableBundle
import androidx.appcompat.app.AppCompatActivity


/**
 *
 * 高阶函数
 *
 * */
fun num1Andnum2(num1: Int, num2: Int, opeation: (Int, Int) -> Int): Int {
    val result = opeation(num1, num2)
    return result
}

fun plus(num1: Int, num2: Int): Int {
    return num1 + num2
}

fun minus(num1: Int, num2: Int): Int {
    return num1 - num2
}

fun main() {
    println(" ${num1Andnum2(15, 7, ::minus)}")


    val num1Andnum2 = num1Andnum2(70, 59) { n1, n2 ->
        n1 + n2
    }

    println("num1Andnum2: $num1Andnum2")



    main1()
}

fun main1() {
    println("start----")
    testFunc("xixi") {
        println("start")
        if (it.isEmpty()) return@testFunc //testFunc函数用inline关键字修饰，可以去掉@testFunc
        println("$it")
        println("end")
    }
    println("end----")
}

inline fun inlineTestd(block: () -> Unit, noinline block2: () -> Unit) {

}

fun testFunc(str: String, block: (String) -> Unit) {
    block(str)
}

inline fun f(crossinline body: () -> Unit) {
    val f = object : Runnable {
        override fun run() = body()
    }
}

class Senior:AppCompatActivity(){
    @SuppressLint("CommitPrefEdits")
    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)

//        val edit = getSharedPreferences("data", Context.MODE_PRIVATE).edit()
//        edit.putString("name","hm")
//        edit.putInt("age",15)
//        edit.apply()


        getSharedPreferences("data",Context.MODE_PRIVATE).open {
            putString("name","hm")
            putInt("age",13)
        }
    }
}

@SuppressLint("CommitPrefEdits")
fun SharedPreferences.open(block:SharedPreferences.Editor.() ->Unit){
    val edit = edit()
    edit.block()
    edit.apply()
}
